home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / cpp_libs / nihcl-30.lha / nihcl-3.0 / errfac / ErrFac.c < prev    next >
C/C++ Source or Header  |  1990-05-15  |  4KB  |  149 lines

  1. /* ErrFac.c -- member functions of class ErrorFacility
  2.  
  3.     THIS SOFTWARE FITS THE DESCRIPTION IN THE U.S. COPYRIGHT ACT OF A
  4.     "UNITED STATES GOVERNMENT WORK".  IT WAS WRITTEN AS A PART OF THE
  5.     AUTHOR'S OFFICIAL DUTIES AS A GOVERNMENT EMPLOYEE.  THIS MEANS IT
  6.     CANNOT BE COPYRIGHTED.  THIS SOFTWARE IS FREELY AVAILABLE TO THE
  7.     PUBLIC FOR USE WITHOUT A COPYRIGHT NOTICE, AND THERE ARE NO
  8.     RESTRICTIONS ON ITS USE, NOW OR SUBSEQUENTLY.
  9.  
  10. Author:
  11.     S.M. Orlow
  12.     Systex,Inc.
  13.     Beltsville, MD 20705
  14.     301-474-0111
  15.     June, 1986
  16.  
  17. Contractor:
  18.     K. E. Gorlen
  19.     Bg. 12A, Rm. 2017
  20.     Computer Systems Laboratory
  21.     Division of Computer Research and Technology
  22.     National Institutes of Health
  23.     Bethesda, Maryland 20892
  24.     Phone: (301) 496-5363
  25.     uucp: uunet!nih-csl!kgorlen
  26.     Internet: kgorlen@alw.nih.gov
  27.  
  28. Function:
  29.  
  30. Class for describing all error messages for a particular facility.
  31.     
  32. $Log:    ErrFac.c,v $
  33.  * Revision 3.0  90/05/15  22:36:51  kgorlen
  34.  * Release for 1st edition.
  35.  * 
  36. */
  37.  
  38. static const char rcsid[] = "$Header: /afs/alw.nih.gov/unix/sun4_40c/usr/local/src/nihcl-3.0/share/errfac/RCS/ErrFac.c,v 3.0 90/05/15 22:36:51 kgorlen Rel $";
  39.  
  40. #include "ErrFac.h"
  41. #include <string.h>
  42.  
  43. #define DEFAULT_EXPANSION_FACTOR 2
  44. #define expand_by(F,X) (X) + (X)/(F)
  45. #define DEFAULT_FACILITY_CAPACITY 16
  46.  
  47. const char* left_cmt = "/*";
  48. const char* rght_cmt = "*/";
  49.  
  50. ErrorSpecs :: ErrorSpecs(int err_sev,char* err_args,char* err_text) {
  51.     severity = err_sev;
  52.     args = new char[strlen(err_args)+1];
  53.     strcpy(args,err_args);
  54.     text = new char[strlen(err_text)+1];
  55.     strcpy(text,err_text);
  56.     }
  57.  
  58. void ErrorSpecs :: printOn(ostream& strm) {
  59.     strm << severity << ", \042" << args << "\042, " << text;
  60.     }
  61.  
  62. ErrorFacility :: ErrorFacility(char* long_name,char* short_name,
  63.                 int fac_code,char* base_name) {
  64.     longname  = new char[strlen(long_name)+1];
  65.     strcpy(longname,long_name);
  66.     shortname  = new char[strlen(short_name)+1];
  67.     strcpy(shortname,short_name);
  68.     basename  = new char[strlen(base_name)+1];
  69.     strcpy(basename,base_name);
  70.     Code = fac_code;
  71.  
  72.     capacity = DEFAULT_FACILITY_CAPACITY;
  73.     errlist = new ErrorSpecsPt[capacity];
  74.     size = 0;
  75.     }
  76.  
  77. void ErrorFacility :: resize(int newcapacity) {
  78.     if ( newcapacity <= capacity ) return;
  79.     ErrorSpecsPt* tmp = errlist;
  80.     errlist = new ErrorSpecsPt[newcapacity];
  81.  
  82.     for(int i=0; i<=capacity; i++)
  83.       errlist[i] = tmp[i];
  84.  
  85.     capacity = newcapacity;
  86.     delete tmp;
  87.     }
  88.  
  89. void ErrorFacility :: add(ErrorSpecs& sp) {
  90. //cerr << "ErrorFacility: capacity=" << capacity << " size = " << size << "\n";
  91.     if ( size == capacity ) 
  92.       resize(expand_by(DEFAULT_EXPANSION_FACTOR,capacity));
  93.     errlist[size++] = &sp;
  94.     }
  95. void ErrorFacility :: h_init(ostream& strm) {
  96.     strm << "#ifndef " << basename << "_H\n"
  97.          << "#define " << basename << "_H\n";
  98.     strm << left_cmt << "\n"
  99.           << "* " << basename << ".h\n"
  100.           << "* C include file created by errgen utility.  DO NOT EDIT!\n"
  101.          << rght_cmt << "\n";
  102.  
  103.     strm << left_cmt << "\n *     ***** " << longname 
  104.               << " facility *****\n " << rght_cmt << "\n";
  105.  
  106.     strm << "\nextern void " << shortname << "errinit();\n\n";
  107.     }
  108.  
  109. void ErrorFacility :: h_define(ostream& strm,char* name,int val) {
  110.     strm << "#define " << shortname << name << "\011\011" << val << "\n";
  111.     }
  112.  
  113. void ErrorFacility :: h_finish(ostream& strm) {
  114.     strm << "#endif\n";
  115.     }
  116.  
  117. void ErrorFacility :: printOn(ostream& strm) {
  118.     strm << left_cmt << "\n" << " * " << basename << ".c --"
  119.              << " Error definition file generated by errgen.  DO NOT EDIT!\n"
  120.          << rght_cmt << "\n";
  121.     strm << "#include <errors.h>\n"
  122.          << "#include \042" << basename << ".h\042\n"
  123.          << "extern ErrFac errfac[MAX_FACILITIES];\n";
  124.     strm << left_cmt << "\n"
  125.          << "*    ***** " << longname << " facility *****\n"
  126.          << rght_cmt << "\n";
  127.     strm << "ErrSpecs " << shortname << "errlist[] = {\n";
  128.  
  129.     for(int i=0; i<size; i++) {
  130.       strm << " {";
  131.       errlist[i]->printOn(strm);
  132.       strm << " }";
  133.       if ( i < size-1 ) strm << ",";
  134.       strm << "\n";
  135.       };
  136.     strm << "};\n";
  137.  
  138.     strm << left_cmt << "\n"
  139.          << "* Error initialization routine for " << longname 
  140.          << " facility.\n" << rght_cmt << "\n";
  141.  
  142.     strm << "void " << shortname << "errinit() {\n"
  143.          << "errfac[" << Code << "].last = " 
  144.          << shortname << "LAST_ERROR;\n"
  145.          << "errfac[" << Code << "].longname = \042" <<longname<< "\042;\n"
  146.          << "errfac[" << Code << "].errlist = " << shortname <<"errlist;\n"
  147.          << "};\n";
  148.     }
  149.